/**
 * Copyright 2014-2020  the original author or authors.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.upb.webserver.config;

import com.upb.webserver.common.pojo.base.BasePageResponse;
import com.upb.webserver.common.pojo.base.BaseResponse;
import com.upb.webserver.common.pojo.config.ReqAddConfigVO;
import com.upb.webserver.common.pojo.config.RspConfigTypeVO;
import com.upb.webserver.common.pojo.config.RspConfigVO;
import com.upb.webserver.common.returncode.ConstantCode;
import com.upb.webserver.common.tools.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Log4j2
@RestController
@RequestMapping(value = "config")
public class ConfigController {

    @Autowired
    private ConfigService configService;


    /**
     * @return
     */
    @GetMapping(value = "type/all")
    public BaseResponse getAllConfigType() {
        log.info("start exec method [getAllConfigType]");
        List<RspConfigTypeVO> typeList = configService.getConfigTypeList();

        BaseResponse baseResponse = new BaseResponse(ConstantCode.SUCCESS, typeList);
        log.info("success exec method [listConfig] success, result:{}", JsonUtils.objToString(baseResponse));
        return baseResponse;
    }


    /**
     * @param param
     * @param result
     * @return
     */
    @PostMapping
    public BaseResponse addConfig(@RequestBody @Valid ReqAddConfigVO param, BindingResult result) {
        log.info("start exec method [addConfig]. param:{}", JsonUtils.objToString(param));
        configService.addConfig(param.getConfigValue(), param.getConfigType());
        log.info("success exec method [addConfig].");
        return new BaseResponse(ConstantCode.SUCCESS);
    }


    /**
     * 获取配置类型
     */
    @GetMapping(value = "list")
    public BasePageResponse listConfigByPage(@RequestParam("pageNumber") Integer pageNumber,
                                             @RequestParam("pageSize") Integer pageSize,
                                             @RequestParam(value = "type", required = false) Byte type) {
        log.info("start exec method [listConfigByPage], type:{}", type);

        log.info("list config,  type: [{}]", type);
        BasePageResponse basePageResponse = configService.listConfigByType(pageNumber, pageSize, type);

        log.info("success exec method [listConfigByPage] success, result:{}", JsonUtils.objToString(basePageResponse));
        return basePageResponse;
    }


    /**
     * @param configId
     * @return
     */
    @DeleteMapping(value = "/{configId}")
    public BaseResponse deleteConfig(@PathVariable("configId") Integer configId) {
        log.info("start exec method [deleteConfig]. configId:{}", configId);
        configService.deleteById(configId);
        log.info("success exec method [deleteConfig]");
        return new BaseResponse(ConstantCode.SUCCESS);
    }
}

